#ifndef _CAR_H
#define _CAR_H
#ifdef  __cplusplus
    extern "C" {
#endif
/*-----------------------------------------------------------------------
|                               Includes                                |
-----------------------------------------------------------------------*/
#include "main.h"
#include "Motor.h"
#include "tim.h"
#include "adc.h"
#include "iMath.h"
#include "board_base.h"
#include "car_ctrl.h"
#include "bsp_uart.h"
//#define SERVO_ENABLE 0
#if SERVO_ENABLE == 1
#include "Servo.h"
#endif
/*-----------------------------------------------------------------------
|                                define                                 |
-----------------------------------------------------------------------*/
#define CarDebugf(format,...) UARTx_Printf(&huart2,  (format), ##__VA_ARGS__)
#define CarDebug(str) UARTx_Print(&huart2,  (str))

typedef enum Car_Ctrl_Mode
{
	CARMode_Stop,
	CARMode_Track,  //循迹
	CARMode_Manual, // 直线
	CARMode_Patrol, // 巡线

	CARMode_MAX
} CarMode_e;
typedef enum Car_Ctrl_State
{
	CARStop_State,
	CARManual_State,
	CARTrack_State
} CarState_e;
typedef struct Car_Structure
{
	CarMode_e mode; //小车模式
	CarState_e state;
	Motor_t *pML;	// 右轮
	Motor_t *pMR;	// 左轮
	uint8_t DisplayEN:1;
    ADC_HandleTypeDef *phadcx;//ADC接口
	uint16_t powerADC; 	// 电量
	uint16_t power;		//滤波后的电量
	float powerV;		//电量换算为电压
	int baseSpeed;
	float PID_P;		//循迹环
	float PID_I;
	float PID_D;
	uint8_t XCnt;//根据路程设定速度的计数器
	uint8_t XTraceEN:1;
	uint8_t XTotal;
	uint32_t PatrolTime;//巡线时间结果
	uint32_t PatrolTimeCounter;//记录时间的变量
	float angle;
	float _angleV;
	float angleV;
} Car_t;

typedef struct 
{
    int32_t xl;//0~1M
    int32_t xr;
    int16_t  t;//阈值
    uint16_t v;
}Xtrace_t;

//typedef struct
//{
//	uint16_t data;
//	int16_t position;
//	int16_t _position;//上次位置
//	int8_t errR;
//	int8_t errL;
//} Tracker_t;
#define TraceBoardCtrl(x) (x == 0 ? \
(TraceEN_GPIO_Port->BRR |= TraceEN_Pin) : (TraceEN_GPIO_Port->BSRR |= TraceEN_Pin))
#define TraceBoardState() (TraceEN_GPIO_Port->IDR & TraceEN_Pin)
/*-----------------------------------------------------------------------
|                             API FUNCTION                              |
-----------------------------------------------------------------------*/
extern Car_t car;
extern  Tracker_t  traceData ;

void Car_Init(void);
void Car_Start(void);
void Car_Set_Rate(int rateL, int rateR);
inline void Car_Set_Speed(int speedL, int speedR);
void Car_Brake(void);//小车刹车
void Car_Stop(void); //熄火，拉手刹
void Car_CheckPower(void);
void CarCtrl_CMD(uint8_t *pdata, int len);
void Car_Patrol(void);
void Car_Patrol_Start(void);
void Car_Patrol_Stop(void);

void X_Trace_Save(void);
void Send_Panel(void);
uint16_t  WS_Car_Get_Track_Date(void);//  读取循迹管数据
#ifdef __cplusplus
}
#endif
#endif
